package com.mall507.fivepeaks.activity.community;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.Message;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 协会详情 | 动态
 * @date 2019/8/8
 */
@Route(path = "/community/CommunityAssociationInfoDynamicRUI")
public class CommunityAssociationInfoDynamicRUI extends AbsRecyclerViewUI<Message> {
    
    @ViewInject(R.id.collection) //收藏
    LinearLayout collection;
    @ViewInject(R.id.img_collection) //收藏图片
    ImageView img_collection;
    @ViewInject(R.id.lickNumber)
    TextView lickNumber;

    List<String> listUrl;
    View headView;
    private int page = 0;
    
    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_community_association_info_dynamic;
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("详情");
        lickNumber.setText(getIntent().getStringExtra("collection_count"));
        headView = getLayoutInflater().inflate(R.layout.head_rv_community_association_info_dynamic, null);
        getAdapter().setHeaderView(headView);
        getComment();
        views();
        isCollection();
        collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int collection_count = Integer.parseInt(lickNumber.getText().toString());
                if (page == 0){
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_blue));
                    lickNumber.setText(String.valueOf(collection_count+ 1));
                    setCollection();
                    page = 1;
                }else if (page == 1){
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_hui));
                    lickNumber.setText(String.valueOf(collection_count+ -1));
                    cancelCollection();
                    page = 0;
                }
            }
        });
    }

    @Override
    protected String setHttpPath() {
        return "Comment/getAssociationComment";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<Message> handleHttpData(JSONObject response) {
        List<Message> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray data = response.optJSONArray("data");
            if (data != null && data.length() > 0) {
                for (int i = 0; i < data.length(); i++) {
                    JSONObject object = data.optJSONObject(i);
                    JSONObject member = data.optJSONObject(i).optJSONObject("member");
                    Message model = new Message();
                    model.setNid(object.optString("id"));
                    if (member != null && member.length()>0){
                        model.setHead_img(member.optString("head_img"));
                        model.setNickname(member.optString("nickname"));
                        model.setCreate_at(member.optString("create_at"));
                    }
                    model.setContent(object.optString("desc"));
                    list.add(model);
                }
            }
        }
        return list;
    }


    private void getComment() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Association/getAssociationActivity", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                getTitleView().setContent(data.optString("title"));
                ((TextView)headView.findViewById(R.id.head_title)).setText(data.optString("title")); //活动标题
                ((TextView)headView.findViewById(R.id.head_content)).setText(data.optString("desc")); //活动内容
                ((TextView)headView.findViewById(R.id.head_pageview)).setText(data.optString("views") + "人浏览"); //活动浏览量
                long ctime = Long.parseLong(data.optString("ctime"));
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(ctime*1000);
                ((TextView)headView.findViewById(R.id.head_time)).setText(simpleDateFormat.format(date)); //活动发布时间

                JSONArray pics=data.optJSONArray("imgs");
                List<String> listUrl=new ArrayList<>();
                for(int i=0;i<pics.length();i++){
                    listUrl.add(pics.optString(i));
                }
                bindTopBannerData(listUrl);
            }
        });
    }
    /**
     * 绑定banner数据
     */
    private void bindTopBannerData(List<String> listUrl) {
        BGABanner banner = headView.findViewById(R.id.head_banner);
        banner.setAdapter(new BGABanner.Adapter<View, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, View itemView, String url, int position) {
                SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.sdv_item_fresco_content);
                simpleDraweeView.setImageURI(url);
            }
        });
        banner.setData(R.layout.homerecycle_top_banner_content, listUrl, null);
    }


    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_comn_goods_item_info;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, Message model) {
        holder.setText(R.id.name, model.getNickname());
        holder.setText(R.id.desc, model.getContent());
        holder.setText(R.id.time, model.getCreate_at());
        ((CircleImageView) holder.getView(R.id.icon)).setImageURI(model.getHead_img());
    }

    private void views(){
        BaseHttp.getInstance().query("Action/viewBlog", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
            }
        });
    }

    private void isCollection(){
        BaseHttp.getInstance().query("Action/isCollection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (response.optInt("data") == 1){
                    page = 1;
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_blue));
                }else {
                    page = 0;
                    img_collection.setImageDrawable(getResources().getDrawable(R.mipmap.like_hui));
                }
                super.onSuccess(response);
            }
        });
    }

    private void setCollection(){
        BaseHttp.getInstance().query("Action/collection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
            }
        });
    }

    private void cancelCollection(){
        BaseHttp.getInstance().query("Action/cancelCollection", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
            }
        });
    }

    private JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("type"));
            params.put("id", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }
    
}
